import pinyin from 'pinyin-match'
import { parseRegex } from '.'

export const searchWinPlugins = (plugins: plugin.Plugin[], winState: WinState) => {
  const ret: plugin.PluginElem[] = []

  plugins.forEach((plugin) => {
    plugin.config.features.forEach((feature) => {
      const cmd = feature.cmds.find((cmd) => {
        if (typeof cmd !== 'string' && cmd.type === 'window') {
          return cmd.match.app.find((name) => name === winState.appName)
        }
        return false
      })

      if (cmd) {
        ret.push({
          name: plugin.name,
          dev: plugin.dev,
          icon: feature.icon ?? plugin.config.logo,
          code: feature.code,
          cmd,
          label: typeof cmd === 'string' ? cmd : cmd.label
        })
      }
    })
  })

  return ret
}

export const searchPlugins = (plugins: plugin.Plugin[], state: IClipboardItem | string) => {
  const ret: plugin.PluginElem[] = []
  if (typeof state === 'string') {
    state = {
      id: '',
      copyTime: 0,
      type: 'text',
      content: state
    }
  }

  plugins.forEach((plugin) => {
    plugin.config.features.forEach((feature) => {
      const cmd = feature.cmds.find((cmd) => {
        if (state.type === 'text') {
          const keyword = state.content
          if (typeof cmd === 'string') {
            return pinyin.match(cmd, keyword)
          } else if (cmd.type === 'regex') {
            const { minLength, maxLength, match, exclude } = cmd
            if (
              (minLength && keyword.length < minLength) ||
              (maxLength && keyword.length > maxLength)
            ) {
              return false
            }
            if (exclude) {
              if (parseRegex(exclude).test(keyword)) {
                return false
              }
            }

            if (match) {
              return parseRegex(match).test(keyword)
            }
            return true
          }
        }

        if (typeof cmd !== 'string') {
          if (cmd.type === 'img' && state.type === 'img') {
            return true
          } else if (cmd.type === 'files' && state.type === 'file') {
            return state.files.find((file) => {
              //类型
              if (cmd.fileType === 'file' && !file.isFile) {
                return false
              } else if (cmd.fileType === 'dir' && !file.isFile) {
                return false
              }

              //文件名匹配
              if (cmd.match) {
                const reg = parseRegex(cmd.match)
                if (!reg.test(file.name)) {
                  return false
                }
              }
              //文件名长度匹配
              if (cmd.minLength && file.name.length < cmd.minLength) {
                return false
              }

              if (cmd.maxLength && file.name.length > cmd.maxLength) {
                return false
              }
              return true
            })
          }
        }

        return false
      })

      if (cmd) {
        ret.push({
          name: plugin.name,
          dev: plugin.dev,
          icon: feature.icon ?? plugin.config.logo,
          code: feature.code,
          cmd,
          label: typeof cmd === 'string' ? cmd : cmd.label
        })
      }
    })
  })

  // //win state
  // plugins.forEach((plugin) => {
  //   plugin.config.features.forEach((feature) => {
  //     const cmd = feature.cmds.find((cmd) => {
  //       if (typeof cmd !== 'string' && cmd.type === 'window') {
  //         return cmd.match.app.find((name) => name === winState.appName)
  //       }
  //       return false
  //     })

  //     if (cmd) {
  //       ret.push({
  //         name: plugin.name,
  //         dev: plugin.dev,
  //         icon: 'image://' + join(plugin.baseDir, feature.icon ?? plugin.config.logo),
  //         code: feature.code,
  //         cmd,
  //         label: typeof cmd === 'string' ? cmd : cmd.label
  //       })
  //     }
  //   })
  // })

  return ret
}

export const findPlugin = (plugins: plugin.Plugin[], ele: plugin.PluginElem) => {
  return plugins.find(
    (plugin) =>
      plugin.name === ele.name &&
      plugin.config.features.some(
        (feature) =>
          feature.code === ele.code &&
          feature.cmds.some(
            (cmd) =>
              (typeof cmd === 'string' && cmd === ele.cmd) ||
              (typeof ele.cmd !== 'string' &&
                typeof cmd !== 'string' &&
                cmd.label === ele.cmd.label)
          )
      )
  )
}
